package com.ruoyi.production.service.impl.greenhousefilm;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.domain.greenhousefilm.GreenhousefilmAmount;
import com.ruoyi.production.mapper.graftingclip.GraftingclipAmountMapper;
import com.ruoyi.production.mapper.greenhousefilm.GreenhousefilmAmountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.greenhousefilm.GreenhousefilmPurchaseMapper;
import com.ruoyi.production.domain.greenhousefilm.GreenhousefilmPurchase;
import com.ruoyi.production.service.greenhousefilm.IGreenhousefilmPurchaseService;

/**
 * 棚膜购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class GreenhousefilmPurchaseServiceImpl implements IGreenhousefilmPurchaseService
{
    @Autowired
    private GreenhousefilmPurchaseMapper greenhousefilmPurchaseMapper;
    @Autowired
    private GreenhousefilmAmountMapper greenhousefilmAmountMapper;

    /**
     * 查询棚膜购买信息
     *
     * @param greenhousefilmPurchaseId 棚膜购买信息ID
     * @return 棚膜购买信息
     */
    @Override
    public GreenhousefilmPurchase selectGreenhousefilmPurchaseById(Long greenhousefilmPurchaseId)
    {
        return greenhousefilmPurchaseMapper.selectGreenhousefilmPurchaseById(greenhousefilmPurchaseId);
    }

    /**
     * 查询棚膜购买信息列表
     *
     * @param greenhousefilmPurchase 棚膜购买信息
     * @return 棚膜购买信息
     */
    @Override
    public List<GreenhousefilmPurchase> selectGreenhousefilmPurchaseList(GreenhousefilmPurchase greenhousefilmPurchase)
    {
        return greenhousefilmPurchaseMapper.selectGreenhousefilmPurchaseList(greenhousefilmPurchase);
    }

    /**
     * 新增棚膜购买信息
     *
     * @param greenhousefilmPurchase 棚膜购买信息
     * @return 结果
     */
    @Override
    public int insertGreenhousefilmPurchase(GreenhousefilmPurchase greenhousefilmPurchase)
    {
        return greenhousefilmPurchaseMapper.insertGreenhousefilmPurchase(greenhousefilmPurchase);
    }

    /**
     * 修改棚膜购买信息
     *
     * @param greenhousefilmPurchase 棚膜购买信息
     * @return 结果
     */
    @Override
    public int updateGreenhousefilmPurchase(GreenhousefilmPurchase greenhousefilmPurchase)
    {
        Long greenhousefilmPurchaseId = greenhousefilmPurchase.getGreenhousefilmPurchaseId();
        //数据库存储的数据
        GreenhousefilmPurchase PurchaseDB = selectGreenhousefilmPurchaseById(greenhousefilmPurchaseId);
        //数据库存储的原数据的总量
        GreenhousefilmAmount originAmounts = new GreenhousefilmAmount();
        originAmounts.setThickness(PurchaseDB.getThickness());
        originAmounts.setGreenhousefilmTypeId(PurchaseDB.getGreenhousefilmTypeId());
        originAmounts.setOriginCompany(PurchaseDB.getOriginCompany());
        List<GreenhousefilmAmount> amounts = greenhousefilmAmountMapper.selectGreenhousefilmAmountList(originAmounts);
        GreenhousefilmAmount originAmount = amounts.get(0);
        if (PurchaseDB.getThickness().equals(greenhousefilmPurchase.getThickness()) &&
                PurchaseDB.getOriginCompany().equals(greenhousefilmPurchase.getOriginCompany()) &&
                PurchaseDB.getGreenhousefilmTypeId().equals(greenhousefilmPurchase.getGreenhousefilmPurchaseId())
        ) {
            originAmount.setStorageAmount(originAmount.getStorageAmount()+greenhousefilmPurchase.getPurchaseAmount()-PurchaseDB.getPurchaseAmount());
            greenhousefilmAmountMapper.updateGreenhousefilmAmount(originAmount);
        }else {
            GreenhousefilmAmount newAmounts = new GreenhousefilmAmount();
            newAmounts.setThickness(greenhousefilmPurchase.getThickness());
            newAmounts.setGreenhousefilmTypeId(greenhousefilmPurchase.getGreenhousefilmTypeId());
            newAmounts.setOriginCompany(greenhousefilmPurchase.getOriginCompany());
            List<GreenhousefilmAmount> amounts1 = greenhousefilmAmountMapper.selectGreenhousefilmAmountList(newAmounts);
            if (StringUtils.isEmpty(amounts1)){
                newAmounts.setStorageAmount(greenhousefilmPurchase.getPurchaseAmount());
                greenhousefilmAmountMapper.insertGreenhousefilmAmount(newAmounts);
            }else {
                GreenhousefilmAmount newAmount = amounts1.get(0);
                newAmount.setStorageAmount(newAmount.getStorageAmount()+greenhousefilmPurchase.getPurchaseAmount());
                greenhousefilmAmountMapper.updateGreenhousefilmAmount(newAmount);
            }

            originAmount.setStorageAmount(originAmount.getStorageAmount()-PurchaseDB.getPurchaseAmount());
            greenhousefilmAmountMapper.updateGreenhousefilmAmount(originAmount);
        }
        return greenhousefilmPurchaseMapper.updateGreenhousefilmPurchase(greenhousefilmPurchase);
    }

    /**
     * 批量删除棚膜购买信息
     *
     * @param greenhousefilmPurchaseIds 需要删除的棚膜购买信息ID
     * @return 结果
     */
    @Override
    public int deleteGreenhousefilmPurchaseByIds(Long[] greenhousefilmPurchaseIds)
    {
        for (Long greenhousefilmPurchaseId : greenhousefilmPurchaseIds) {
            GreenhousefilmPurchase greenhousefilmPurchase = selectGreenhousefilmPurchaseById(greenhousefilmPurchaseId);
            GreenhousefilmAmount greenhousefilmAmount = new GreenhousefilmAmount();
            greenhousefilmAmount.setThickness(greenhousefilmPurchase.getThickness());
            greenhousefilmAmount.setGreenhousefilmTypeId(greenhousefilmPurchase.getGreenhousefilmTypeId());
            greenhousefilmAmount.setOriginCompany(greenhousefilmPurchase.getOriginCompany());
            List<GreenhousefilmAmount> amounts = greenhousefilmAmountMapper.selectGreenhousefilmAmountList(greenhousefilmAmount);
            GreenhousefilmAmount greenhousefilmAmount1 = amounts.get(0);
            greenhousefilmAmount1.setStorageAmount(greenhousefilmAmount1.getStorageAmount() - greenhousefilmPurchase.getPurchaseAmount());
            greenhousefilmAmountMapper.updateGreenhousefilmAmount(greenhousefilmAmount1);
        }
        return greenhousefilmPurchaseMapper.deleteGreenhousefilmPurchaseByIds(greenhousefilmPurchaseIds);
    }

    /**
     * 删除棚膜购买信息信息
     *
     * @param greenhousefilmPurchaseId 棚膜购买信息ID
     * @return 结果
     */
    @Override
    public int deleteGreenhousefilmPurchaseById(Long greenhousefilmPurchaseId)
    {
        GreenhousefilmPurchase greenhousefilmPurchase = selectGreenhousefilmPurchaseById(greenhousefilmPurchaseId);
        GreenhousefilmAmount greenhousefilmAmount = new GreenhousefilmAmount();
        greenhousefilmAmount.setThickness(greenhousefilmPurchase.getThickness());
        greenhousefilmAmount.setGreenhousefilmTypeId(greenhousefilmPurchase.getGreenhousefilmTypeId());
        greenhousefilmAmount.setOriginCompany(greenhousefilmPurchase.getOriginCompany());
        List<GreenhousefilmAmount> amounts = greenhousefilmAmountMapper.selectGreenhousefilmAmountList(greenhousefilmAmount);
        GreenhousefilmAmount greenhousefilmAmount1 = amounts.get(0);
        greenhousefilmAmount1.setStorageAmount(greenhousefilmAmount1.getStorageAmount() - greenhousefilmPurchase.getPurchaseAmount());
        greenhousefilmAmountMapper.updateGreenhousefilmAmount(greenhousefilmAmount1);
        return greenhousefilmPurchaseMapper.deleteGreenhousefilmPurchaseById(greenhousefilmPurchaseId);
    }

    /**
     * 插入棚膜购买表更新棚膜量表
     * @param greenhousefilmPurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(GreenhousefilmPurchase greenhousefilmPurchase) {
        GreenhousefilmAmount greenhousefilmAmount = new GreenhousefilmAmount();
        greenhousefilmAmount.setThickness(greenhousefilmPurchase.getThickness());
        greenhousefilmAmount.setGreenhousefilmTypeId(greenhousefilmPurchase.getGreenhousefilmTypeId());
        greenhousefilmAmount.setOriginCompany(greenhousefilmPurchase.getOriginCompany());
        List<GreenhousefilmAmount> greenhousefilmAmounts = greenhousefilmAmountMapper.selectGreenhousefilmAmountList(greenhousefilmAmount);
        greenhousefilmAmount.setStorageAmount(greenhousefilmPurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(greenhousefilmAmounts)) {
            GreenhousefilmAmount amount = greenhousefilmAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount() + greenhousefilmPurchase.getPurchaseAmount());
            greenhousefilmAmountMapper.updateGreenhousefilmAmount(amount);
        } else {
            greenhousefilmAmountMapper.insertGreenhousefilmAmount(greenhousefilmAmount);
        }
        return insertGreenhousefilmPurchase(greenhousefilmPurchase);
    }
}
